package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.product.mapper.BaseTrademarkMapper;
import com.atguigu.gmall.product.model.BaseCategoryTrademark;
import com.atguigu.gmall.product.mapper.BaseCategoryTrademarkMapper;
import com.atguigu.gmall.product.model.BaseTrademark;
import com.atguigu.gmall.product.model.CategoryTrademarkVo;
import com.atguigu.gmall.product.service.BaseCategoryTrademarkService;
import com.atguigu.gmall.product.service.BaseTrademarkService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 分类品牌中间表 业务实现类
 *
 * @author atguigu
 * @since 2023-04-18
 */
@Service
public class BaseCategoryTrademarkServiceImpl extends ServiceImpl<BaseCategoryTrademarkMapper, BaseCategoryTrademark> implements BaseCategoryTrademarkService {

    @Autowired
    private BaseTrademarkService baseTrademarkService;


    /**
     * 根据category3Id获取品牌列表
     * @param category3Id
     * @return
     */
    @Override
    public List<BaseTrademark> getTrademarkListByCategoryId(Long category3Id) {
        //根据分类品牌关系表id查询，获取当前分类关联所有品牌集合
        LambdaQueryWrapper<BaseCategoryTrademark> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaseCategoryTrademark::getCategory3Id,category3Id);
        List<BaseCategoryTrademark> baseCategoryTrademarkList = this.list(queryWrapper);
        //判断集合是否有值
        if (!CollectionUtils.isEmpty(baseCategoryTrademarkList)){
            //获取品牌id 转化成Long类型
            List<Long> trademarkList = baseCategoryTrademarkList.stream().map(BaseCategoryTrademark::getTrademarkId).collect(Collectors.toList());
            //根据品牌id集合查询品牌集合
            List<BaseTrademark> baseTrademarkList = baseTrademarkService.listByIds(trademarkList);
            return baseTrademarkList;
        }
        return null;
    }

    /**
     * 根据category3Id获取可选品牌列表
     * @param category3Id
     * @return
     */
    @Override
    public List<BaseTrademark> getCurrentTrademarkList(Long category3Id) {
        //自定义sql 子查询-关联查询
        BaseTrademarkMapper trademarkMapper = (BaseTrademarkMapper) baseTrademarkService.getBaseMapper();
        List<BaseTrademark> baseTrademarkList = trademarkMapper.getCurrentTrademarkList(category3Id);
        return baseTrademarkList;
    }

    /**
     * 保存分类品牌关联
     * @return
     */
    @Override
    public void saveCategoryTrademark(CategoryTrademarkVo categoryTrademarkVo) {
        //获取要关联到当前分类品牌id集合
        List<Long> trademarkIdList = categoryTrademarkVo.getTrademarkIdList();

        if (!CollectionUtils.isEmpty(trademarkIdList)){
            //转为分类品牌关系对象集合
            List<BaseCategoryTrademark> categoryTrademarkList = trademarkIdList.stream().map(trademarkId -> {
                //为当前品牌构建一个分类对象
                BaseCategoryTrademark baseCategoryTrademark = new BaseCategoryTrademark();
                //为两个属性分别赋值
                baseCategoryTrademark.setCategory3Id(categoryTrademarkVo.getCategory3Id());
                baseCategoryTrademark.setTrademarkId(trademarkId);
                return baseCategoryTrademark;
                //把处理之后的数据收集起来
            }).collect(Collectors.toList());
            //批量执行保存分类品牌集合
            this.saveBatch(categoryTrademarkList);
        }
    }

    /**
     * 删除分类品牌关联
     * @param category3Id
     * @param trademarkId
     * @return
     */
    @Override
    public void deleteCategoryTrademark(Long category3Id, Long trademarkId) {
        //设置条件
        LambdaQueryWrapper<BaseCategoryTrademark> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaseCategoryTrademark::getCategory3Id,category3Id);
        queryWrapper.eq(BaseCategoryTrademark::getTrademarkId,trademarkId);
        //逻辑删除
        this.remove(queryWrapper);
    }
}
